package com.rrd.dw.mr.gzip.kafka;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.third.bairong.BairongPortraitRecord;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import java.io.IOException;

/*
 * 百融客制化评分1反欺诈模型
 *
 * @author:szw
 * */

public class BaiRongPortraitRecordParse extends ParseBase{
    private Text val = new Text();
    private Text key = new Text();

    /**
     * @param context
     * @param model
     * @param successful
     * @return
     * @throws IOException
     * @throws InterruptedException
     */

    @Override
    public boolean parseJob(Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {
        BairongPortraitRecord brRecord = (BairongPortraitRecord) model;
        JSONObject brData = brRecord.getData() == null ? new JSONObject() : JSON.parseObject(brRecord.getData());

        StringBuilder sb = getBaseInfo(brRecord, successful);
        StringBuilder brSb = new StringBuilder(2000);
        // code
        brSb.append(brData.getString("code") == null ? "" : brData.getString("code")).append(CRT001);
        // swift_number
        brSb.append(brData.getString("swift_number") == null ? "" : brData.getString("swift_number")).append(CRT001);
        if (brData.getString("Flag") != null) {
            JSONObject brFlag = brData.getString("Flag") == null ? new JSONObject() : JSON.parseObject(brData.getString("Flag"));
            // Flag/score
            brSb.append(brFlag.getString("score") == null ? "" : brFlag.getString("score")).append(CRT001);
            brSb.append(brFlag.getString("scoredata") == null ? "" : brFlag.getString("scoredata")).append(CRT001);
        } else {
            brSb.append(CRT001);
            brSb.append(CRT001);
        }
        if (brData.getString("Score") != null) {
            JSONObject brScore = brData.getString("Score") == null ? new JSONObject() : JSON.parseObject(brData.getString("Score")) ;
            // Score/scorecust1
            brSb.append(brScore.getString("scorecust1") == null ? "" : brScore.getString("scorecust1")).append(CRT001);
        } else {
            brSb.append(CRT001);
        }
        JSONObject brScoreData = brData.getString("ScoreData") == null ? new JSONObject() : JSON.parseObject(brData.getString("ScoreData"));
        JSONObject brScoreCust1 = brScoreData.getString("scorecust1") == null ? new JSONObject() : JSON.parseObject(brScoreData.getString("scorecust1"));
        // sd/scorecust1/alf_apirisk_time_area_d180_mean
        brSb.append(brScoreCust1.getString("alf_apirisk_time_area_d180_mean")).append(CRT001);
        // sd/scorecust1/alf_ict_lenth_d15_mean
        brSb.append(brScoreCust1.getString("alf_ict_lenth_d15_mean")).append(CRT001);
        // sd/scorecust1/alf_ict_lenth_d15_sum
        brSb.append(brScoreCust1.getString("alf_ict_lenth_d15_sum")).append(CRT001);
        // sd/scorecust1/als_diff_coon_a
        brSb.append(brScoreCust1.getString("als_diff_coon_a")).append(CRT001);
        // sd/scorecust1/als_diff_n_nsloan_a
        brSb.append(brScoreCust1.getString("als_diff_n_nsloan_a")).append(CRT001);
        // sd/scorecust1/als_diff_rel_o
        brSb.append(brScoreCust1.getString("als_diff_rel_o")).append(CRT001);
        // sd/scorecust1/als_fst_id_nbank_inteday
        brSb.append(brScoreCust1.getString("als_fst_id_nbank_inteday")).append(CRT001);
        // sd/scorecust1/als_lst_cell_bank_inteday
        brSb.append(brScoreCust1.getString("als_lst_cell_bank_inteday")).append(CRT001);
        // sd/scorecust1/als_m1_cell_caon_orgnum
        brSb.append(brScoreCust1.getString("als_m1_cell_caon_orgnum")).append(CRT001);
        // sd/scorecust1/als_m1_cell_nbank_orgnum
        brSb.append(brScoreCust1.getString("als_m1_cell_nbank_orgnum")).append(CRT001);
        // sd/scorecust1/als_m1_weekday_end_pro_a
        brSb.append(brScoreCust1.getString("als_m1_weekday_end_pro_a")).append(CRT001);
        // sd/scorecust1/als_m1_weekday_pro_o
        brSb.append(brScoreCust1.getString("als_m1_weekday_pro_o")).append(CRT001);
        // sd/scorecust1/als_m12_cell_nbank_max_inteday
        brSb.append(brScoreCust1.getString("als_m12_cell_nbank_max_inteday")).append(CRT001);
        // sd/scorecust1/als_m12_id_bank_max_inteday
        brSb.append(brScoreCust1.getString("als_m12_id_bank_max_inteday")).append(CRT001);
        // sd/scorecust1/als_m12_id_bank_min_inteday
        brSb.append(brScoreCust1.getString("als_m12_id_bank_min_inteday")).append(CRT001);
        // sd/scorecust1/als_m12_id_bank_tra_allnum
        brSb.append(brScoreCust1.getString("als_m12_id_bank_tra_allnum")).append(CRT001);
        // sd/scorecust1/als_m12_id_nbank_max_inteday
        brSb.append(brScoreCust1.getString("als_m12_id_nbank_max_inteday")).append(CRT001);
        // sd/scorecust1/als_m12_id_nbank_nsloan_allnum
        brSb.append(brScoreCust1.getString("als_m12_id_nbank_nsloan_allnum")).append(CRT001);
        // sd/scorecust1/als_m12_id_oth_allnum
        brSb.append(brScoreCust1.getString("als_m12_id_oth_allnum")).append(CRT001);
        // sd/scorecust1/als_m12_n_else_a
        brSb.append(brScoreCust1.getString("als_m12_n_else_a")).append(CRT001);
        // sd/scorecust1/als_m3_id_max_inteday
        brSb.append(brScoreCust1.getString("als_m3_id_max_inteday")).append(CRT001);
        // sd/scorecust1/als_m3_off_pro_a
        brSb.append(brScoreCust1.getString("als_m3_off_pro_a")).append(CRT001);
        // sd/scorecust1/als_m3_weekday_end_pro_o
        brSb.append(brScoreCust1.getString("als_m3_weekday_end_pro_o")).append(CRT001);
        // sd/scorecust1/als_m6_b_pro_o
        brSb.append(brScoreCust1.getString("als_m6_b_pro_o")).append(CRT001);
        // sd/scorecust1/als_m6_cell_bank_min_inteday
        brSb.append(brScoreCust1.getString("als_m6_cell_bank_min_inteday")).append(CRT001);
        // sd/scorecust1/als_m6_weekday_pro_o
        brSb.append(brScoreCust1.getString("als_m6_weekday_pro_o")).append(CRT001);
        // sd/scorecust1/frg_list_level
        brSb.append(brScoreCust1.getString("frg_list_level")).append(CRT001);
        // sd/scorecust1/gl_m12_beyond8_hit_months
        brSb.append(brScoreCust1.getString("gl_m12_beyond8_hit_months")).append(CRT001);
        // sd/scorecust1/gl_m12_max_group_num
        brSb.append(brScoreCust1.getString("gl_m12_max_group_num")).append(CRT001);
        // sd/scorecust1/gl_m12_mean_list_level
        brSb.append(brScoreCust1.getString("gl_m12_mean_list_level")).append(CRT001);
        // sd/scorecust1/gl_m6_mean_list_level
        brSb.append(brScoreCust1.getString("gl_m6_mean_list_level")).append(CRT001);
        // sd/scorecust1/gl_m6_orgType_5_6_months
        brSb.append(brScoreCust1.getString("gl_m6_orgType_5_6_months")).append(CRT001);
        // sd/scorecust1/ir_id_x_cell_notmat_days
        brSb.append(brScoreCust1.getString("ir_id_x_cell_notmat_days")).append(CRT001);
        // sd/scorecust1/ir_m6_id_x_cell_cnt
        brSb.append(brScoreCust1.getString("ir_m6_id_x_cell_cnt")).append(CRT001);
        // sd/scorecust1/pd_cell_city_new_house_sort
        brSb.append(brScoreCust1.getString("pd_cell_city_new_house_sort")).append(CRT001);
        // sd/scorecust1/pd_cell_city_rent_sort
        brSb.append(brScoreCust1.getString("pd_cell_city_rent_sort")).append(CRT001);
        // sd/scorecust1/pd_id_apply_age
        brSb.append(brScoreCust1.getString("pd_id_apply_age")).append(CRT001);
        // sd/scorecust1/pd_id_city_new_house_sort
        brSb.append(brScoreCust1.getString("pd_id_city_new_house_sort")).append(CRT001);
        // sd/scorecust1/pd_id_city_old_house_sort
        brSb.append(brScoreCust1.getString("pd_id_city_old_house_sort")).append(CRT001);
        // sd/scorecust1/pd_id_gender
        brSb.append(brScoreCust1.getString("pd_id_gender")).append(CRT001);
        // sd/scorecust1/sd_scoremixuals_uals_lst_inteday
        brSb.append(brScoreCust1.getString("sd_scoremixuals_uals_lst_inteday")).append(CRT001);

        StringBuilder txSb = getBaseInfo(brRecord);
        key.set(getKey());
        val.set((sb.toString() + brSb.toString() + txSb.toString()).replace("\n", " ").replace("\r", " "));
        context.write(key, val);

        return true;
    }

    @Override
    public String getTableName() {
        return "kafka_babel_br_portrait_record_inc";
    }

    @Override
    public String getType() {
        return RecordType.BAIRONG_PORTRAIT_RECORD.toString();
    }

    @Override
    public String getComment() {
        return "百融客制化评分1反欺诈模型";
    }
}
